package au.com.cmcrc.tradelab.pm;

/**
 * @author Fawad Nazir (fawad.nazir@gmail.com)
 *
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import au.com.cmcrc.tradelab.pm.datafeed.StockEntity;
import au.com.cmcrc.tradelab.pm.datafeed.yahoo.YahooDataDownload;
import au.com.cmcrc.tradelab.pm.environment.ConnectionValidator;


/**
 * @author Fawad Nazir (fawad.nazir@gmail.com)
 *
 */
public class Util {

	static Logger logger = Logger.getLogger(Util.class);
	static Properties properties = new Properties();
	static SessionFactory sessionFactory;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Util.init();
		Util.applicationEnvironmentValidators();
				
		
		ArrayList<String> srr=new ArrayList<String>();
		
		srr.add("XOM");
		srr.add("BBDb.TO");
		srr.add("JNJ");
		srr.add("MSFT");
		srr.add("MMM");
		srr.add("ACE");
		srr.add("ABT");
		srr.add("ANF");
		srr.add("ADBE");
		srr.add("AMD");
		srr.add("AES");
		srr.add("AET");
		srr.add("AFL");
		srr.add("A");
		srr.add("APD");
		srr.add("ARG");
		
		try{
			YahooDataDownload YahooDataDownloadObj = new YahooDataDownload(srr);
			System.out.println(YahooDataDownloadObj.getUrl());
			ArrayList<StockEntity> stockEntityVal = YahooDataDownloadObj.download();
			
			for(int i=0;i<stockEntityVal.size();i++){
				StockEntity StockEntityObj = stockEntityVal.get(i);
				System.out.println(StockEntityObj.getStockSymbol()+" "+StockEntityObj.getStockValue());
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		/*
		try{
			Util.setUp();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		*/


	}
	
	public static void setUp() throws Exception {
		Session session = null;

		try{
			// This step will read hibernate.cfg.xml and prepare hibernate for use
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			 session =sessionFactory.openSession();
			 Transaction tx = session.beginTransaction();

				
		}catch(Exception e){ 
			System.out.println(e.getMessage());
		}finally{
			// Actual contact insertion will happen at this step
			session.flush();
			System.out.println("Flush");
			session.close();

			}
	}
	
	public static void init(){
		
		PropertyConfigurator.configure("conf/log4j.properties");
		logger.info("Initializing the application.");
		
		try {
			properties.load(new FileInputStream("conf/app.properties"));
		} catch (IOException e) {
			logger.error("Error loading the app.properties file: " + e.getMessage());
		}
		
	}
	
	public static void applicationEnvironmentValidators(){
		
		boolean FAIL = false;
		
		if(!ConnectionValidator.isInternetReachable(Util.getProperty("url.test.yahoo"))){
			FAIL = true;
		}
		if(!ConnectionValidator.isInternetReachable(Util.getProperty("url.test.google"))){
			FAIL = true;
		}
		if(!ConnectionValidator.isInternetReachable(Util.getProperty("url.test.yahoo.finance"))){
			FAIL = true;
		}
		if(!ConnectionValidator.isInternetReachable(Util.getProperty("url.test.google.finance"))){
			FAIL = true;
		}
		
		if(FAIL){
			System.exit(0);
		}
		
		//Test DB Connection.
		
	}
	
	public static String getProperty(String propertyName){
		return properties.getProperty(propertyName);
	}

}
